import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
import { LoginUserService } from '../login-user/login-user.service';
import { UserService } from '../user/user.service';
import * as crypto from 'crypto';
function md5(str) {
    const hash = crypto.createHash('md5');
    hash.update(str);
    return hash.digest('hex');
  }
@Injectable()
export class AuthService {
  @Inject()
  private loginUserService: LoginUserService;

  async validateUser(username: string, pass: string) {
    const user = await this.loginUserService.findOneByUsername(username);
    if (!user) {
      throw new UnauthorizedException('用户不存在');
    }
    if (user.password !== md5(pass)) {
      throw new UnauthorizedException('密码错误');
    }
    const { password, ...result } = user;
    return result;
  }
}
